package com.soft.line.service.impl;

import com.github.pagehelper.PageInfo;
import com.soft.line.entity.ButtonInfo;
import com.soft.line.entity.MenuInfo;
import com.soft.line.entity.RoleInfo;
import com.soft.line.filter.HttpRequestUtil;
import com.soft.line.filter.UserTokenDto;
import com.soft.line.mapper.ButtonInfoMapper;
import com.soft.line.mapper.MenuInfoMapper;
import com.soft.line.mapper.RoleInfoMapper;
import com.soft.line.mapper.RoleMenuMapper;
import com.soft.line.service.RoleInfoService;
import com.soft.line.util.pagehelper.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RoleInfoServiceImpl implements RoleInfoService {

    @Autowired
    private RoleInfoMapper roleInfoMapper;
    @Autowired
    private MenuInfoMapper menuInfoMapper;
    @Autowired
    private ButtonInfoMapper buttonInfoMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<RoleInfo> selectAll() {
        return roleInfoMapper.selectAll(null);
    }

    @Override
    public PageInfo<List<Map<String, Object>>> selectAllRoleMenuInfo(Map<String, Object> map) {
        PageUtil.setPageParam(map);
        List<Map<String, Object>> reultList = new ArrayList<Map<String, Object>>();
        for (RoleInfo roleInfo:roleInfoMapper.selectAll((String)map.get("roleName"))){
            Map<String, Object> roleMap = new HashMap<String, Object>();
            roleMap.put("id",roleInfo.getId());
            roleMap.put("roleName",roleInfo.getName());
            roleMap.put("state",roleInfo.getState());
            List<Map<String, Object>> menuList = new ArrayList<Map<String, Object>>();
            for (MenuInfo menuInfo:menuInfoMapper.selectByRoleId(roleInfo.getId())){
                Map<String, Object> menuMap = new HashMap<String, Object>();
                menuMap.put("menuId",menuInfo.getId());
                menuMap.put("menuName",menuInfo.getMenuName());
                List<ButtonInfo> buttonInfos=buttonInfoMapper.selectButtonsByRoleIdAndMenuId(roleInfo.getId(),menuInfo.getId());
                menuMap.put("buttonList",buttonInfos);
                menuList.add(menuMap);
            }
            roleMap.put("menuList",menuList);
            reultList.add(roleMap);
        }
        PageInfo<List<Map<String, Object>>> resultPage = new PageInfo(reultList);
        return resultPage;
    }

    @Override
    public void delete(Map<String, Object> map) {
        Integer roleId =(Integer)map.get("id");
        roleMenuMapper.deleteByRoleId(roleId);
        roleInfoMapper.delete(roleId);
    }

    @Override
    public int countRoleName(Integer roleId, String roleName) {
        return roleInfoMapper.countRoleName(roleId,roleName);
    }

    @Override
    public void add(Map<String, Object> map) {
        String roleName = (String)map.get("roleName");
        String modifyPerson = "admin";
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if(userToken!=null){
            modifyPerson=userToken.getUserInfo().getUserName();
        }
        map.put("modifyPerson",modifyPerson);
        map.put("modifyTime",new Date());
        roleInfoMapper.insert(map);
        RoleInfo roleInfo = roleInfoMapper.selectByRoleName(roleName);
        List<Map<String, Object>> menuList=(List)map.get("menu");
        for (Map<String, Object> mp:menuList){
            Integer menuId=(Integer)mp.get("menuId");
            for (String buttonId:(List<String>)mp.get("buttonList")){
                roleMenuMapper.insert(roleInfo.getId(),menuId,buttonId,modifyPerson);
            }
        }
    }

    @Override
    public void update(Map<String, Object> map) {
        Integer roleId =(Integer)map.get("id");
        String modifyPerson = "admin";
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if(userToken!=null){
            modifyPerson=userToken.getUserInfo().getUserName();
        }
        map.put("modifyPerson",modifyPerson);
        map.put("modifyTime",new Date());
        roleInfoMapper.update(map);
        roleMenuMapper.deleteByRoleId(roleId);
        List<Map<String, Object>> menuList=(List)map.get("menu");
        for (Map<String, Object> mp:menuList){
            Integer menuId=(Integer)mp.get("menuId");
            for (String buttonId:(List<String>)mp.get("buttonList")){
                roleMenuMapper.insert(roleId,menuId,buttonId,modifyPerson);
            }
        }
    }
}
